<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: CommonPlatformRequest.php
 * +----------------------------------------------------------------------
 * | Author: sam
 * +----------------------------------------------------------------------
 * | Description: 公共平台请求类
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
 * +----------------------------------------------------------------------
 * | 1.Date: 2018/12/25 Author: sam Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Library\Guzzle;

use App\Constant\Compatible;
use App\Constant\ThirdCommonCode;
use App\Constants\RedisKey;
use App\Library\Traits\PlatformRequest;
use App\Models\RepayRecordCommon;
use App\Models\RepaySummary;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class CommonPlatformRequest
{
    use PlatformRequest;
    /**
     * 公共平台基础uri
     * @var \Illuminate\Config\Repository|mixed
     */
    protected $platform_uri;

    /**
     * 短信验证码接口url
     * @var string
     */
    protected $sms_url = '/message/sms';


    /**
     * 绑定银行卡发送短信验证码接口url
     * @var string
     */
    protected $bingCardSendSmsUrl = '/pay/agreement/applyBindCard';

    /**
     * 绑卡签约确认url
     * @var string
     */
    protected $bindCardConfirmUrl = '/pay/agreement/bindCardConfirm';

    /**
     * 验证协议号是否有效
     * @var string
     */
    protected $checkHoldAgreementUrl = '/pay/agreement/checkAgreementNo';
    /**
     * 解绑银行卡url
     * @var string
     */
    protected $unbindCardUrl = 'pay/agreement/unBindCard';

    /**
     * 查询银行卡是否是信用卡url
     * @var string
     */
    protected $bankCodeUrl = '/bank/bankCode';

    /**
     * 公共支付接口url接口
     * @var string接口描述 分类 描述 方法名
     */
    protected $common_pay = '/pay/agreement/withholdUseThirdPartyAgrNo';

    /**
     * 支付协议订单查询接口
     *
     * @var string
     */
    protected $get_common_repay_result = '/pay/queryOrder';

    /**
     * 爬虫状态查询接口
     *
     * @var string
     */
    protected $get_risk_status_info = '/datacenter/riskStatusInfo';

    /**
     * 通话详单前30条查询
     *
     * @var string
     */
    protected $query_top_phone_details = '/riskControl/queryTopCall';


    /**
     * 公共请求参数
     * @var array
     */
    protected $body = [];

    /**
     * 参数集合
     * @var
     */
    private $_pub_data;

    /**
     * 业务数据签名
     * @var
     */
    private $_pub_sign;

    /**
     * CommonPlatformRequest constructor.
     */
    public function __construct()
    {
        $this->platform_uri = config('common.common_platform_base_uri');
    }

    /**
     * Description:发送短信接口
     * Author: Sam
     * DateTime: 2019/1/2 10:59
     * @param $phone
     * @param $templateId
     * @param $map
     * @return mixed
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sms($phone, $templateId,$map)
    {
        $this->_pub_data = [
            'type' => '2',
            'templateId' => $templateId,
            'messageParamMap' => $map,
            'phoneList' => [$phone],
        ];
        $this->_pub_sign = Sign::sign($this->_pub_data, 1);
        $url = $this->sms_url;
        return $this->request($url,'common');
    }

    /**
     * Description: 队列发送短信
     * Author: liuju
     * DateTime: 2019-01-14 11:53
     * @param $templateDetail
     * @param $phone
     * @return mixed
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function smsQueueSend($phone, $templateDetail=[])
    {
        if(is_array($phone)){
            $phone = array_map(function($val){
                return strval($val);
            },$phone);
        }else{
            $phone = [strval($phone)];
        }
        $this->_pub_data = [
            'type' => '2',
            'phoneList' => $phone,
        ];
        $this->_pub_data = array_merge($this->_pub_data,$templateDetail);
        $this->_pub_sign = Sign::sign($this->_pub_data, 1);
        $url = $this->sms_url;
        return $this->request($url,'common');
    }


    /**
     * Description: 绑卡发送信息验证申请
     * Author: liuju
     * DateTime: 2019-01-02 11:23
     * @param $cardNum 银行卡号
     * @param $certNum  身份证号
     * @param $phoneNum 银行卡手机号码
     * @param $realName 姓名
     * @return mixed
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function applyBindCard($cardNum, $certNum, $phoneNum, $realName)
    {
        $this->_pub_data = [
            'bankCardNo' => strval($cardNum),
            'certNo' => strval($certNum),
            'phone' => strval($phoneNum),
            'realName' => strval($realName),
        ];
        $this->_pub_sign = Sign::sign($this->_pub_data, 1);
        return $this->request($this->bingCardSendSmsUrl,'common');
    }

    /**
     * Description: 确认绑定银行卡操作
     * Author: liuju
     * DateTime: 2019-01-02 14:02
     * @param $cardNum  银行卡号
     * @param $certNum 身份证号
     * @param $smsCode 短信验证码
     * @param $orderNo 订单号
     * @param $phone  银行卡手机号码
     * @param $realName 姓名
     * @return mixed
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function bindCardConfirm($cardNum, $certNum, $smsCode, $orderNo, $phone, $realName)
    {
        $this->_pub_data = [
            'bankCardNo' => strval($cardNum),
            'certNo' => strval($certNum),
            'dynamicCode' => strval($smsCode),
            'orderNo' => strval($orderNo),
            'phone' => strval($phone),
            'realName' => strval($realName)
        ];
        $key = RedisKey::BANK_CARD_REQUEST_DATA;
        Redis::lPush($key,json_encode($this->_pub_data,JSON_UNESCAPED_UNICODE));
        $this->_pub_sign = Sign::sign($this->_pub_data, 1);
        return $this->request($this->bindCardConfirmUrl,'common');
    }


    /**
     * Description:验证协议号是否被绑定过
     * Author: liuju
     * DateTime: 2019-01-02 17:33
     * @param $holdAgreementNum
     * @return mixed
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function checkHoldAgreement($holdAgreementNum)
    {
        $this->_pub_data = [
            'withholdAgreementNo' => strval($holdAgreementNum),
        ];
        $this->_pub_sign = Sign::sign($this->_pub_data, 1);
        return $this->request($this->checkHoldAgreementUrl,'common');
    }

    /**
     * Description: 解绑用户银行卡
     * Author: liuju
     * DateTime: 2019-01-02 17:27
     * @param $holdAgreementNum   协议号
     * @return mixed
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function unbindCard($holdAgreementNum)
    {
        $this->_pub_data = [
            'withholdAgreementNo' => strval($holdAgreementNum),
        ];
        $this->_pub_sign = Sign::sign($this->_pub_data, 1);
        return $this->request($this->unbindCardUrl,'common');

    }


    /**
     * Description:  获取银行卡相关信息：银行卡名称、银行卡全称、银行卡编码，是否是信用卡
     * Author: liuju
     * DateTime: 2019-01-03 20:05
     * @param $bankNum
     * @return mixed
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public  function  bankCode($bankNum)
    {
         $this->_pub_data =[
             'cardNo' => $bankNum,
         ];
         $this->_pub_sign = Sign::sign($this->_pub_data,1);
         return $this->request($this->bankCodeUrl,'common');
    }

    /**
     * 请求公共支付接口
     */
    public function setCommonRepay($data)
    {
        $amount = sprintf("%.2f", $data['withholdTradeAmount']);
        $amount = floatval($amount);
        $this->_pub_data =[
            'thirdPartyAgreementNo' => $data['thirdPartyAgreementNo'],//支付渠道协议号
            'thirdPartyClientId' => $data['thirdPartyClientId'],//第三方商户号
            'withholdTradeAmount' => $amount,//支付金额,单位为元
            'withholdTradeNo' => $data['withholdTradeNo'],//业务系统协议支付唯一编号
            'withholdTradeSubject' => $data['withholdTradeSubject'],//协议支付标题
            'remark' => isset($data['remark']) ? $data['remark'] : '',//备注
            'bizOrderNoPrefix' => isset($data['bizOrderNoPrefix']) ? $data['bizOrderNoPrefix'] : RepayRecordCommon::COMMON_PREFIX,
        ];
        $dataType = RepaySummary::getDataTypeByLoanId($data['loanId']);
        $this->_pub_sign = Sign::sign($this->_pub_data,1,$dataType);
        $result = $this->request($this->common_pay,'common',$dataType);
        return $result;
    }

    /**
     * Description: 公共扣款结果查询
     * Author: Gxs
     * @param $data
     * @return bool|mixed
     */
    public function getCommonRepayResult($data)
    {
        $this->_pub_data =[
            'channelId' => $data['channelId'], //支付渠道
            'orderId' => $data['orderId'], //用户流水号
        ];
        $this->_pub_sign = Sign::sign($this->_pub_data,1);
        $result = $this->request($this->get_common_repay_result,'common');
        Log::debug('repay_get_common_api', ['result' => $result]);
        if ($result['code'] != ThirdCommonCode::CODE_SUCCESS) {
            return false;
        }
        return $result;
    }

    /**
     * Description: 爬虫状态查询接口
     * Author: Gxs
     * @param $data
     * @return bool|mixed
     */
    public function getRiskStatusInfo($data)
    {
        $this->_pub_data =[
            'tradeNo' => $data['tradeNo'], //支付渠道
        ];
        $this->_pub_sign = Sign::sign($this->_pub_data,1);
        $result = $this->request($this->get_risk_status_info,'common');
        Log::debug('risk_get_status_info_api', ['result' => $result]);
//        if ($result['code'] != ThirdCommonCode::CODE_SUCCESS) {
//            return false;
//        }
        return $result;
    }

    public function queryTopPhoneDetails($data)
    {
        $this->_pub_data =[
            'idNo' => $data['idNo'], //身份证号
            'phone' => $data['phone'], //手机号
            'count' => 30
        ];

        $this->_pub_sign = Sign::sign($this->_pub_data,1);
        $result = $this->request($this->query_top_phone_details,'common');
        return $result;
    }
}